package edu.ncu.controller;

import edu.ncu.entity.User;
import edu.ncu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
//配置需要在session中存放的数据范围
//@SessionAttributes(value = {"user"} )
public class UserController {

    @RequestMapping("/hello")
    public String hello(ModelMap map)
    {
        map.put("thText", "设置文本内容");
        map.put("thUText", "设置文本内容");
        map.put("thValue", "设置当前元素的value值");
        map.put("thEach", Arrays.asList("列表", "遍历列表"));
        map.put("thIf", "msg is not null");
        return "test";
    }

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    //验证登录
    @RequestMapping("/doLogin")
    public String doLogin(String account, String upwd, HttpSession session)
    {
        System.out.println("帐号："+account +" 密码："+upwd);
        User user = userService.findByAccount(account);
        //判断账号密码是否正确
        if(userService.judgePwd(account, upwd))
        {
            //把User加进session
            session.setAttribute("user", user);
            return "redirect:/newsSquare?userId="+user.getUserId();
        }
        else
            return "redirect:/error";
    }



    //退出登录
    @RequestMapping("/signOut")
    public String signOut(HttpSession session)
    {
        System.out.println("当前登录的帐号：" + session.getAttribute("user"));
        //毁灭session
        session.removeAttribute("user");
//        session.invalidate();
//        return "redirect:/news/findAll";
        return "redirect:/newsSquare_loggedOut";
    }

    //注册
    @RequestMapping("/register")
    public String register(User user)
    {
        System.out.println("注册收到的user："+user);
        userService.saveUser(user);
        return "/login";
    }

    //更换头像(未完成)
    @RequestMapping("/updateHeadPhoto")
    public String updateHeadPhoto(HttpServletRequest request, MultipartFile upload, Model model)
    {
        System.out.println("更换头像");
        String path = request.getSession().getServletContext().getRealPath("/uploads");
        File file = new File(path);
        if(!file.exists())
            file.mkdirs();
        String filename = upload.getOriginalFilename();

        return "/admin";
    }

    //搜索用户（加好友）
    @RequestMapping("/findByFuzzy")
    public String findByFuzzy(String account, String uname, ModelMap modelMap)
    {
        List<User> userList = userService.findByFuzzy(account , uname);
        System.out.println("加好友搜索出来的信息："+userList);
        modelMap.put("userList", userList);
        return "/test";
    }

    //修改个人资料
    @RequestMapping("/updateUserDetail")
    public String updateUserDetail(User user, HttpSession session)
    {
        System.out.println("获取到的user："+user);
        userService.updateUser(user);
        session.setAttribute("user",userService.findByUserId(user.getUserId()));
        return "redirect:/personDetail";
    }


    @RequestMapping("/search")
    public String search(int userId,String account,Model model) {
        // 返回theFriends
        List<User> theFriends = userService.findLikeAccount(account);
        model.addAttribute("theFriends",theFriends) ;
        User user = (User) userService.findByUserId(userId) ;
        model.addAttribute("user",user) ;
        return "searchUsers" ;
    }
}
